﻿#region File Description

//-----------------------------------------------------------------------------
// CommentController.cs
//
// Author: ITU Team3 (2012)
//-----------------------------------------------------------------------------

#endregion File Description

#region Using Statements

using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using rentit03_client.Models;
using rentit03_client.rentit03;

#endregion

namespace rentit03_client.Controllers
{
    public class CommentController : RentitController
    {
        /// <summary>
        /// Create a comment attached to a media and a user
        /// </summary>
        /// <param name="model">The comment to create</param>
        /// <returns>The <seealso cref="ActionResult"/> containing the create action</returns>
        [HttpPost]
        public ActionResult Create(CommentViewModel model)
        {
            var comment = new CommentObj
            {
                Comment = model.Comment,
                MediaId = model.MediaId,
                UserId = model.UserId
            };

            var r = new ServiceClient().CreateComment(comment.ToJSON(), Sid);

            return RedirectToAction("Index", "Movie", new { @id = model.MediaId });
        }

        /// <summary>
        /// Deletes a specific comment
        /// </summary>
        /// <param name="commentId">The id of the comment to delete</param>
        /// <returns>The <seealso cref="ActionResult"/> containing the delete action</returns>
        public ActionResult Delete(int commentId)
        {
            var r = new ServiceClient().DeleteComment(commentId, Sid);
            var ro = MagicJSON.Deserialize<ReturnObj>(r);

            return RedirectToAction("Index", "Movie", new { @id = ro.Success ? ro.Value : String.Empty });
        }
    }

    public class CommentViewModel
    {
        public int Id { get; set; }

        [Required]
        public int MediaId { get; set; }

        [Required]
        public int UserId { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        public string Comment { get; set; }

        [DataType(DataType.DateTime)]
        public string Created { get; set; }

        [DataType(DataType.DateTime)]
        public string Updated { get; set; }
    }
}